import React,{Component} from 'react';
//import {Link} from 'react-router'
import Pages from '../Pages'
import { Spin,message,Button} from 'antd';
import * as pub from '../../config/public';
import * as fetch from '../../config/fetch';
import '../../style/less/privateLetter.less';
import {browserHistory} from 'react-router';
import Layer from '../Layer'
class PrivateLetter extends Component {
	constructor(args) {
		super()
		this.state = {
			privateLetterRusult:[],
			jumpPage: 1,
			totalPage: 1,
			showLayer:false,
			privateletterId:''
		}
			
	}
	show(){
		let now_userType = localStorage.getItem("now_userType");
		let pageno = this.state.jumpPage
		pub.loading(this,true);
		fetch.addresseePrivateLetter(pageno,now_userType).then((data)=>{
			if(data.result === 'RC100'){
				this.setState({
					privateLetterRusult:data.privateLetterRusult?data.privateLetterRusult:[],
					totalPage:data.totalPage
				})
			}else{
               message.error(data.errMsg, 3);
			}
			pub.loading(this,false);
		},(res) => {
			pub.loading(this, false);
			pub.reject(res);
		})
	}
	componentWillMount(){
		this.show()	
	}
	refreshInfo(){
		this.show()
	}
	delletter(Id){
		pub.loading(this,true);
		fetch.delletter({privateletterId:Id}).then((data)=>{
			if(data.result === 'RC100'){
				this.show();
				this.setState({
					showLayer:false
				})
			}else{
				message.error(data.errMsg, 3);
			}
			pub.loading(this,false);
		},(res) => {
			pub.loading(this, false);
			pub.reject(res);
		})
	}
	jump(page) {
		this.setState({
			jumpPage: page
		}, () => {
			this.show();
		})
	}
	routers(id){
		let data = {infoSuccess:false}
			let path = {
				pathname:"/PrivateDialogue/" + id,
				state: data
			}
			pub.jumpHistory.push(path)
	}
	delletterShow(id){
		this.setState({
			showLayer:true,
			privateletterId:id
		})
	}
	hideModel(){
		this.setState({
			showLayer:false
		})
	}
	render(){
		return(
			<div className="wrapper bg-F0F4F6 privateLetter">
				<div className="layout clearFix pad-t-20">
					
					<div className="row common-title" style={{padding: '10px 30px'}}>我的私信
						<button className="sx-refresh" onClick={()=>this.refreshInfo()}>刷新信息</button>
					</div>
					<Spin spinning={this.state.loading} tip="加载列表中...">
						<div className="sx-lists">
							{this.state.privateLetterRusult.map((item,index)=>{
								return(
									<ul className="clearFix" key={index}>
										<li>和{item.userRealName}对话</li>
										<li>
											{
												item.operationState === '1'?
													<span>未读</span>
												:   <span style={{background:'#fff'}}></span>
											}
										</li>
										<li>{pub.formatTimestamp(item.createTime)}</li>
										<li><p>{pub.subString(item.privateletterNote,25)}</p></li>
										<li><a onClick={()=>{this.routers(item.privateletterId)}}>查看</a></li>
									{/*<li><Link to={'/PrivateDialogue/'+item.privateletterId}>查看</Link></li>*/}
										<li><a onClick={()=>{this.delletterShow(item.privateletterId)}}>删除</a></li>
									</ul>
								)
							})}	
						</div>
					</Spin>
                    
					<Layer 
						title = ""
						show={this.state.showLayer} 
						hide={this.hideModel.bind(this)}
						width="300px"
						height="150px"
					>
					    <div className="row">
							<div className="pop-box-1">																					
								<p>你确定要删除对话？</p>										
							</div>
									<div className="pop-btn" style={{padding:'7px 0'}}>
										<Button loading={this.state.loading} onClick={()=>this.hideModel()} className="btn btn-blue">取消</Button>
										<Button loading={this.state.loading} className="btn btn-blue" onClick={()=>{this.delletter(this.state.privateletterId)}}>确认</Button>
									</div>
						</div>
					</Layer>

					<div className="line"></div>
					<Pages totalPage={this.state.totalPage} changePage={this.state.jumpPage} jump={this.jump.bind(this)}/>
				</div>
			</div>
		)
	}
	
}



export default PrivateLetter